package com.future.mayi.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.baselib.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周大虎 on 2017/2/20.
 */

public class BottomTabBar extends LinearLayout implements View.OnClickListener {


    private List<TextView> listTv;

    private int selectTextColor = R.color.bottom_tab_bar_t;
    private int textColor = R.color.bottom_tab_bar_f;

    private int[] ivResourceFalse, ivResourceTrue;
    private Context mContext;

    public BottomTabBar(Context context) {
        super(context);
        init(context);
    }

    public BottomTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BottomTabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    public interface OnItemClickListener {
        void onItem(int onItem);

        void onDoubleClick(int position);
    }


    private OnItemClickListener onItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private void init(Context context) {
        mContext = context;
        View.inflate(context, R.layout.layout_bottom_tabbar, this);

        ivResourceFalse = new int[]{R.drawable.index_bottom_1_f, R.drawable.index_bottom_2_f, R.drawable.index_bottom_3_f, R.drawable.index_bottom_4_f};
        ivResourceTrue = new int[]{R.drawable.index_bottom_1_t, R.drawable.index_bottom_2_t, R.drawable.index_bottom_3_t, R.drawable.index_bottom_4_t};

        listTv = new ArrayList<>();
        listTv.add((TextView) findViewById(R.id.tv_tab_index));
        listTv.add((TextView) findViewById(R.id.tv_tab_sort));
        listTv.add((TextView) findViewById(R.id.tv_shopping_cart));
        listTv.add((TextView) findViewById(R.id.tv_tab_my));


        findViewById(R.id.rl_index).setOnClickListener(this);
        findViewById(R.id.rl_sort).setOnClickListener(this);
        findViewById(R.id.rl_shopping_cart).setOnClickListener(this);
        findViewById(R.id.rl_my).setOnClickListener(this);

        setOnItem(0);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_index:
                setOnItem(0);
                break;
            case R.id.rl_sort:
                setOnItem(1);
                break;
            case R.id.rl_shopping_cart:
                setOnItem(2);
                break;
            case R.id.rl_my:
                setOnItem(3);
                break;
            default:
                break;
        }
    }

    private int onItem = 0;

    private long clickTime;

    public void setOnItem(int item) {
        if (item > 3) {
            return;
        }


        long clickTimes = DateUtils.getTimeMillis();
        if (item == onItem) {
            if ((clickTimes - clickTime) < 500 && onItemClickListener != null)
                onItemClickListener.onDoubleClick(onItem);
        }

        clickTime = clickTimes;


        onItem = item;

        for (int i = 0; i < listTv.size(); i++) {
            Drawable drawable;
            if (i == item) {
                drawable = mContext.getResources().getDrawable(ivResourceTrue[i]);
                listTv.get(i).setTextColor(mContext.getResources().getColor(selectTextColor));
            } else {
                drawable = mContext.getResources().getDrawable(ivResourceFalse[i]);
                listTv.get(i).setTextColor(mContext.getResources().getColor(textColor));
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            listTv.get(i).setCompoundDrawables(null, drawable, null, null);
        }


        if (onItemClickListener != null)
            onItemClickListener.onItem(item);
    }

    public List<TextView> getTvList() {
        return listTv;
    }
}
